import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import personRoutes from './routes/person.routes.js';
import sequelize, { testConnection, syncDatabase } from './config/db.config.js';

const app = express();
const PORT = 3000;

app.use(cors());
app.use(express.json());
app.use(morgan('dev'));

// API 路由
app.use('/api', personRoutes);

// 健康检查接口
app.get('/health', (req, res) => {
  res.json({ status: 'ok' });
});

// 启动服务
(async () => {
  try {
    await testConnection();
    await syncDatabase();
    app.listen(PORT, () => {
      console.log(`服务器已启动，监听端口 ${PORT}`);
    });
  } catch (error) {
    console.error('启动服务器失败:', error);
  }
})();
